<?php

defined('__MAPEXEC__') or die('No direct access allowed');

/**
 * Abstract class used for exporting map data as text, commonly
 * used when exporting as some sort of markup language like HTML,
 * or any XML-based markup language.
 */
abstract class Maps_Export_Text extends Maps_Export_Common {
  
  private $indentLen = 0;
  private $indentChar = ' ';
  private $indentSize = 2;
  private $indentStr = '';
  
  /**
   * Indents the tab stop 1 tab
   * @param   void
   * @return  String  An empty string
   */
  protected function indent() {
  	$this->indentLen += $this->indentSize;
    $this->updateIndentStr();
    return '';
  }
  
  /**
   * Indents the line of text one tab.
   * @param   String  Line of text
   * @return  String  Line of text, indented
   */
  protected function indentLn($str='') {
    $this->indent();
    return $this->writeLn($str);
  }
  
  /**
   * Outdents the tab stop one tab
   * @param   void
   * @return  String  An empty string
   */
  protected function outdent() {
  	$this->indentLen -= $this->indentSize;
    if ($this->indentLen < 0) {
    	$this->indentLen = 0;
    }
    $this->updateIndentStr();
    return '';
  }
  
  /**
   * Outdents the line of text one tab.
   * @param   String  Line of text
   * @return  String  Line of text, outdented
   */
  protected function outdentLn($str='') {
    $this->outdent();
    return $this->writeLn($str);
  }
  
  /**
   * Resets the tabstop indent.
   * @param   void
   * @return  void
   */
  protected function resetIndent() {
    $this->indentLen = 0;
    $this->updateIndentStr();
  }
  
  /**
   * Updates the cached indent string
   * @param   void
   * @return  void
   */
  private function updateIndentStr() {
    if ($this->indentLen > 0) {
  	  $this->indentStr = str_repeat($this->indentChar, $this->indentLen);
    } else {
    	$this->indentStr = '';
    }
  }
  
  /**
   * Writes text, prepending the indent string.
   * @param   String  String to be written
   * @return  String  String with indent text before it.
   */
  public function write($str='') {
    return $this->indentStr . $str;
  }
  
  /**
   * Creates a line of indented text in the output.
   * @param   String  Line of text.
   * @return  String  Line of text, indented to current tab stop
   */
  public function writeLn($string='') {
    return $this->indentStr.$string."\n";
  }
  
}
?>